package cn.fl.carloan.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.carloan.service.IOdrQuatPrpsService;
import cn.fl.carloan.vo.OdrQuatPrpsVO;
import cn.fl.framework.base.context.CurrentThreadContext;

import com.alibaba.fastjson.JSONObject;

import fl.carloan.facade.carloan.api.IOdrPrdBscInfoApiService;

/**
 * @Description: 车抵贷方案表 控制层
 */
@Controller
@RequestMapping("/carloan/odrquatprps")
public class OdrQuatPrpsController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(OdrQuatPrpsController.class);

	/**
	 * OdrQuatPrps 服务层BEAN
	 */
	@Autowired
	private IOdrQuatPrpsService odrQuatPrpsService;

	@Autowired
	private IOdrPrdBscInfoApiService odrPrdBscInfoApiService;


	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "信审修改预约方案.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		String contAmt = request.getParameter("contAmt");//贷款金额
		String lsPd = request.getParameter("lsPd");//贷款期数
		String lsPdUntCd = request.getParameter("lsPdUntCd");//贷款期数单位
		//String eachPayAmt = request.getParameter("eachPayAmt");//每期还款金额
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("方案ID不能为空.");
			BaseLogUtils.info(logger, "modify", "信审修改预约方案,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			//计算每期还款金额 start
			//month    分期月数
			//amt      贷款金额
			DataResultDTO fenQiRes = null;//分期金额返回结果
			String eachPayAmt = "";//每期还款金额
			BaseLogUtils.info(logger, "modify", "调用分期金额请求参数 lsPd：" + lsPd  + ",contAmt:" + contAmt);
			if(StringUtils.isNotEmpty(lsPd) && StringUtils.isNotEmpty(contAmt)){
				fenQiRes = odrPrdBscInfoApiService.getFenQiPlanByMonthAndAmt(Integer.parseInt(lsPd), Double.valueOf(contAmt));
				BaseLogUtils.info(logger, "modify", "调用分期金额返回结果:" + JSONObject.toJSONString(fenQiRes));
			}
			//计算每期还款金额 end
	
			OdrQuatPrpsVO entity = null;
			entity = new OdrQuatPrpsVO();
			entity.setId(Long.valueOf(id));
			if(StringUtils.isNotEmpty(contAmt))
				entity.setContAmt(DecimalUtils.convertYuanToFen(Double.valueOf(contAmt)));
			if(StringUtils.isNotEmpty(lsPd))
				entity.setLsPd(Long.valueOf(lsPd));
			if(StringUtils.isNotEmpty(lsPdUntCd))
				entity.setLsPdUntCd(lsPdUntCd);
			if(fenQiRes != null && fenQiRes.getCode().equals("1") && fenQiRes.getData() != null){
				eachPayAmt = fenQiRes.getData().toString();//返回分
			}
			if(StringUtils.isNotEmpty(eachPayAmt))
				entity.setEachPayAmt(Long.valueOf(eachPayAmt));
			boolean success = this.odrQuatPrpsService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "信审修改预约方案失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			result.setData(eachPayAmt!=null?DecimalUtils.convertFenToYuan(Long.valueOf(eachPayAmt)):0);
			BaseLogUtils.info(logger, "modify", "信审修改预约方案成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "信审修改预约方案失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "信审修改预约方案失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "信审修改预约方案.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

}

